package com.sjz.myfrist;


import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import androidx.appcompat.app.AppCompatActivity;

/***
 *   Activity 之间传送数据
 */
public class ActReceiveResultTwoActivity extends AppCompatActivity   {
    private TextView receivedDataTextView;
    private Button returnButton;
    private  String receivedData ="";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.act_receive_two_result);

        receivedDataTextView = findViewById(R.id.act_receive_receivedDataTextView);
        returnButton = findViewById(R.id.act_receive_returnButton);

        Intent intent = getIntent();  //得到传递到的参数的值
        if (intent != null) {
              receivedData = intent.getStringExtra("data");
            if (receivedData != null) {
                receivedDataTextView.setText(receivedData);
            }
        }

        //发送回传参数
        returnButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // 在按钮点击事件中触发数据回传
                String dataToReturn = "Data from ReceiveDataActivity"+receivedData;
                Intent resultIntent = new Intent();
                resultIntent.putExtra("dataFromReceive", dataToReturn);
                setResult(RESULT_OK, resultIntent);
                finish();//关闭当前的activity
            }
        });

    }



}